﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlTypes;
using GuiLayer.AuctionService;
using System.ServiceModel;

namespace GuiLayer
{
    public partial class Form1 : Form
    {
        private AuctionServiceClient aucService;
        public Form1()
        {
            InitializeComponent();
            aucService = new AuctionServiceClient();
            CreateDisplay.Enabled = false;
            Auction.Enabled = false;
            ArtistTap.Enabled = false;
            opretAktion.Enabled = false;
            ManageArt.Enabled = false;
            ManagePayment.Enabled = false;

            //var result = from a in aucService.GetAllAuctions().ToList()

            //             select new
            //             {
            //                 ID = a.ID,
            //                 StartDato = a.StartDate,
            //                 SlutDato = a.EndDate,
            //                 Kunstner = a.Art1.Artist1.Name
            //             };

            //dgAuc.DataSource = result; //new BindingSource(result, null);
        }

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl.SelectedTab == Auction)
            {
                timer1.Enabled = true;
            }
            else
            {
                timer1.Enabled = false;
            }
        }

        //login start
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            int authorisation = aucService.Login(username, password);
            if (authorisation == 2)
            {
                txtPassword.Text = "Success";
                CreateDisplay.Enabled = true;
                Auction.Enabled = true;
                ArtistTap.Enabled = true;
                opretAktion.Enabled = true;
                ManageArt.Enabled = true;
                ManagePayment.Enabled = true;
                
        }
            else if (authorisation == 1)
            {
                txtPassword.Text = "Success";
                CreateDisplay.Enabled = false;
                Auction.Enabled = true;
                ArtistTap.Enabled = false;
                opretAktion.Enabled = false;
                ManageArt.Enabled = false;
                ManagePayment.Enabled = false;
            }
            else
            {
                txtPassword.Text = "Forkert brugernavn eller kode";
                CreateDisplay.Enabled = false;
                Auction.Enabled = false;
                ArtistTap.Enabled = false;
                opretAktion.Enabled = false;
                ManageArt.Enabled = false;
                ManagePayment.Enabled = false;
            }
        }
        //login ends

        // display start
        private void btnCreateDisplay_Click(object sender, EventArgs e)
        {
            try
            {
                //_artService.CreateDisplay(txtArtistCD, txtTitleCD);
            }
            catch (Exception)
            {

                throw;
            }
        }
        // display end

        //Auction start
        private void timer1_Tick(object sender, EventArgs e)
        {

            ////aucService.GetAllAuctions();

            //var result = from a in aucService.GetAllAuctions()

            //             select new
            //             {
            //                 ID = a.ID,
            //                 StartDato = a.StartDate,
            //                 SlutDato = a.EndDate,
            //                 Kunstner = a.Art.Artist.Name
            //             };

            //dgAuction.DataSource = new BindingSource(result, null);
        }

        private void dgAuction_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            aucService.GetAllAuctions();

            var result = from a in aucService.GetAllAuctions().ToList()

                         select new
                         {
                             ID = a.ID,
                             StartDato = a.StartDate,
                             SlutDato = a.EndDate,
                             Kunstner = a.Art1.Artist1.Name
                         };

            dgAuction.DataSource = result; //new BindingSource(result, null);

            int _search = 0;

            if (dgAuction.SelectedCells.Count > 0)
            {
                int selectedrowindex = dgAuction.SelectedCells[0].RowIndex;

                DataGridViewRow selectedRow = dgAuction.Rows[selectedrowindex];

                _search = Convert.ToInt32(selectedRow.Cells["ID"].Value);
            }

            //var auction = aucService.GetAllAuctions(_search);

            //int userBid = ShowDialog(auction.Picture + "\n" + "Kunstneren: " + auction.Artist + "\n" + "Minimums Pris: " +
            //    auction.MinPrice + "\n" + "Minimums Bud: " + auction.MinBid + "\n" + "Start Tidspunkt: " + auction.StartDate
            //    + "\n" + "Slut Tidspunkt: " + auction.EndDate + "\n" + "Nuværende Bud: " + auction.Salesprice,
            //    "Auktion Nummer: " + auction.ID);

            //_artService.NewBid(userBid);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int var = ShowDialog("Kunstneren: " + "\n" + "Minimums Pris: " + "\n" + "Minimums Bud: " + "\n" + "Start Tidspunkt: "
                 + "\n" + "Slut Tidspunkt: " + "\n" + "Nuværende Bud: ",
                 "Auktion Nummer: ");
            label1.Text = Convert.ToString(var);
        }
        //Auction end

        //Manage artist stats
        private void btnNewArtist_Click(object sender, EventArgs e)
        {

            string _createArtistName = txtArtistName.Text;
            string _createArtistAddress = txtArtistAddress.Text;
            string _createArtistCellphone = txtArtistPhone.Text;

            try
            {

                
               

            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnSearchArtist_Click(object sender, EventArgs e)
        {

        }

        private void btnFindArtist_Click(object sender, EventArgs e)
        {
            try
            {
                //var result = from a in _artService.GetArtist()
                //        where a.name.ToLower().Contains(txtArtistName.Text.ToLower())
                //             select new
                //             {
                //                 ID = a.ID,
                //                 Navn = a.Name,
                //                 Adresse = a.Address,
                //                 Telefon = a.Cellphone
                //             };
                //txtArtistName.Text = result.Name;
                //txtArtistAddress.Text = result.Address;
                //txtArtistPhone.Text = result.Cellphone;
                //txtSalesStats.Text = _artService.GetArtistStats();
            }
            catch (Exception)
            {

                throw;
            }
        }
        //Manage artist ends

        //Create auction starts
        private void btnCreateAuction_Click(object sender, EventArgs e)
        {
            string _createMinPrice = txtMinPrice.Text;
            string _createPriceInterval = txtPriceInterval.Text;
            string _createArtID = txtArtID.Text;
            string _createStart = txtStartOA.Text;
            string _createEnd = txtEndOA.Text;
            string _timeInterval = txtTimeInterval.Text;
            string _timeExtend = txtTimeExt.Text;

            try
            {
                // kald wcf lag!!

                aucService.CreateAuction(Convert.ToDouble(_createMinPrice), Convert.ToDouble(_createPriceInterval), Convert.ToDateTime(_createStart), Convert.ToDateTime(_createEnd), Convert.ToInt32(_createArtID), Convert.ToInt32(_timeInterval), Convert.ToInt32(_timeExtend));
                
            }
            catch (Exception)
            {

                throw;
            }
        }
        //Create auction ends

        //manage art start
        private void btnCreateArt_Click(object sender, EventArgs e)
        {
            string _artName = txtName.Text;
            string _artDescription = txtDescription.Text;
            //Farligt! burde try-catches
            string _artArtist = txtArtist.Text;
            string _artPicture = txtPicture.Text;
            //Farligt! Burde try-catches
            string _artBuyprice = txtBuyprice.Text;
            try
            {
                // kald wcf lag!!

                aucService.CreateArt(_artName, _artDescription, Convert.ToInt32(_artArtist), _artPicture, Convert.ToDouble(_artBuyprice));

            }
            catch (Exception)
            {
                lblAdminArtStatus.Text = "Kunstner skal være et ID og Indkøbspris skal være et tal";
            }
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {

            //var result = from a in _artService.GetAllArtist()
            //             where a.name.ToLower().Contains(txtSearch.Text.ToLower()) 
            //             select new
            //             {
            //                 ID = a.artID,
            //                 Navn = a.name,
            //                 Kunstner = a.Artist,
            //                 Indkøbspris = a.Buyprice,
            //                 Salgspris = a.Salesprice,
            //                 Ejer = a.OwnerID
            //             };

            //dgArtist.DataSource = new BindingSource(result, null);
        }
        
        private static int ShowDialog(string text, string caption)
        {

            Form prompt = new Form();
            prompt.Width = 800;
            prompt.Height = 600;
            prompt.Text = caption;
            Label textLabel = new Label() { Height = 200, Left = 40, Top = 20, Text = text };
            TextBox inputbox = new TextBox() { Left = 350, Top = 20, Width = 400 };
            Button confimation = new Button() { Text = "Bud", Left = 350, Width = 100, Top = 70 };
            Button abortion = new Button() { Text = "Afslut", Left = 500, Width = 100, Top = 70 };
            confimation.Click += (sender, e) =>
            {
                textLabel.Text = "Kunstneren: " + "\n" + "Minimums Pris: " + "\n" + "Minimums Bud: " + "\n" + "Start Tidspunkt: "
                    + "\n" + "Slut Tidspunkt: " + "\n" + "Nuværende Bud: " + inputbox.Text; /*_artService.NewBet(inputbox.Text);*/
            };
            abortion.Click += (sender, e) => { prompt.Close(); };
            prompt.Controls.Add(confimation);
            prompt.Controls.Add(abortion);
            prompt.Controls.Add(textLabel);
            prompt.Controls.Add(inputbox);
            prompt.ShowDialog();
            return Convert.ToInt32(inputbox.Text);

        }

        private void dgAuc_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Auction_Click(object sender, EventArgs e)
        {
            
        }

        
        //manage art ends
    }
}
